{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5f89c9d9",
   "metadata": {},
   "source": [
    "# 安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ca985f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#https://pytorch.org/get-started/locally/\n",
    "!pip install torch torchvision torchaudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f9bac6e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!pip install easyocr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e265e0b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eebcd041",
   "metadata": {},
   "source": [
    "# 读取图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de9af422",
   "metadata": {},
   "outputs": [],
   "source": [
    "import easyocr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81d99ec2",
   "metadata": {},
   "source": [
    "[‘ch_sim’，‘en’]是您要阅读的语言列表。 您可以一次传递几种语言，但并非所有语言都可以一起使用。 英语与每种语言都兼容。\n",
    "\n",
    "用于将模型加载到内存中。 这需要一些时间，但只需要运行一次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd061f2c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "reader = easyocr.Reader(['ch_sim','en'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03fc7762",
   "metadata": {},
   "source": [
    "![photo1](photo1.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a199cf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = reader.readtext('photo1.jpg')\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a8850e",
   "metadata": {},
   "source": [
    "还可以将detail设置为0，以简化输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08965930",
   "metadata": {},
   "outputs": [],
   "source": [
    "result2 = reader.readtext('photo1.jpg', detail = 0)\n",
    "result2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82cb3346",
   "metadata": {},
   "source": [
    "readtext 函数的另一个有用的可选参数是段落。 通过设置paragraph=True，EasyOCR 将尝试将原始结果组合成易于阅读的段落。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3debbff2",
   "metadata": {},
   "outputs": [],
   "source": [
    "result4 = reader.readtext('photo1.jpg', paragraph=True)\n",
    "result4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "721f70cc",
   "metadata": {},
   "source": [
    "如果您没有GPU或GPU的内存不足，则可以通过添加gpu = False在CPU模式下运行它"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e08fc8e3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "reader2 = easyocr.Reader(['ch_sim','en'], gpu=False)\n",
    "result3 = reader2.readtext('photo1.jpg')\n",
    "result3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d5cb4a6",
   "metadata": {},
   "source": [
    "# 画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffbc908b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156fd258",
   "metadata": {},
   "outputs": [],
   "source": [
    "top_left = tuple(result[0][0][0])\n",
    "bottom_right = tuple(result[0][0][2])\n",
    "text = result[0][1]\n",
    "font = cv2.FONT_HERSHEY_SIMPLEX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a07c77c7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(\"photo1.jpg\")\n",
    "img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "img = cv2.rectangle(img,top_left,bottom_right,(0,255,0),2)\n",
    "plt.imshow(img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a7c1f42",
   "metadata": {},
   "outputs": [],
   "source": [
    "img2 = cv2.imread(\"photo1.jpg\")\n",
    "img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)\n",
    "for rst in result:\n",
    "    top_left = tuple(rst[0][0])\n",
    "    bottom_right = tuple(rst[0][2])\n",
    "    img2 = cv2.rectangle(img2,top_left,bottom_right,(0,255,0),2)\n",
    "plt.imshow(img2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0773e4f8",
   "metadata": {},
   "source": [
    "# 发票"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2e69e74",
   "metadata": {},
   "source": [
    "![photo3](photo3.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5295ecdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "result6 = reader.readtext('photo3.jpg', detail=0)\n",
    "result6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33315617",
   "metadata": {},
   "source": [
    "![photo2](photo2.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c5f3373",
   "metadata": {},
   "outputs": [],
   "source": [
    "result5 = reader.readtext('photo2.jpg')\n",
    "result5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "582b7df1",
   "metadata": {},
   "outputs": [],
   "source": [
    "img3 = cv2.imread(\"photo2.jpg\")\n",
    "img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "for rst in result5:\n",
    "    top_left = tuple(rst[0][0])\n",
    "    bottom_right = tuple(rst[0][2])\n",
    "    img3 = cv2.rectangle(img3,top_left,bottom_right,(0,255,0),2)\n",
    "plt.imshow(img3)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd102978",
   "metadata": {},
   "source": [
    "# 手写识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c2582e",
   "metadata": {},
   "source": [
    "![photo4](photo4.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17443d9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = easyocr.Reader(['en']) # need to run only once to load model into memory\n",
    "\n",
    "result6 = reader.readtext(\"photo4.jpg\", allowlist='0123456789')\n",
    "result6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f3cefb5",
   "metadata": {},
   "source": [
    "加载MNIST数据集。使用size=1000对这个数据集进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63d6cb9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d4057da",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "test_dataset = torchvision.datasets.MNIST('./data/', train=False, download=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92a2af89",
   "metadata": {},
   "source": [
    "看看一批测试数据由什么组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04a39c84",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, (data, label) in enumerate(test_dataset):\n",
    "    if i % 1000 == 0:\n",
    "        print(i, type(data), label)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f5619a6",
   "metadata": {},
   "source": [
    "可以使用matplotlib来绘制其中的一些"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ce34392",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "for i, (data, label) in enumerate(test_dataset):\n",
    "    if i % 1000 == 0:\n",
    "      plt.subplot(2,5,int(i/1000)+1)\n",
    "      plt.tight_layout()\n",
    "      plt.imshow(data, cmap='gray', interpolation='none')\n",
    "      plt.title(label)\n",
    "      plt.xticks([])\n",
    "      plt.yticks([])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7689b14c",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = easyocr.Reader(['en']) # need to run only once to load model into memory\n",
    "\n",
    "for i, (data, label) in enumerate(test_dataset):\n",
    "    if i % 1000 == 0:\n",
    "        print(i, type(data), label)\n",
    "        result = reader.readtext(np.array(data), allowlist='0123456789')\n",
    "        print(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "148.993px",
    "width": "244.045px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "426.667px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
